package com.sap.apitest2.deprecated.client;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import com.sap.apitest2.deprecated.service.IService;


public class ParameterTest extends AbstractTest implements IService {

	  public static void main(String args[]) throws Exception {
	        
	    	testRequestJsonGetParameters();
	    	testRequestJsonGetParameter();
	    	testRequestJsonPutParameter();
	    	testRequestJsonRemoveParameter();
	    	
	        System.out.println("\n"); 
	        System.exit(0);
	    }
		private static void testRequestJsonGetParameters()throws Exception{
	    	System.out.println("\n");
	        System.out.println("Sent HTTP GET request to get parameters info, expect JSON");
	        GetMethod get = new GetMethod(getTestHost()+ACTION_PARAMETERS+"/1");
	        get.addRequestHeader("Accept" , "application/json");
	        HttpClient httpclient = new HttpClient();
	       
	        try {
	            int result = httpclient.executeMethod(get); 
	            System.out.println("Response status code: " + result);
	            System.out.println("Response body: ");
	            System.out.println(get.getResponseBodyAsString());
	        } finally {
	            get.releaseConnection();             
	        }
	    }
		private static void testRequestJsonGetParameter()throws Exception{
	    	System.out.println("\n");
	        System.out.println("Sent HTTP GET request to get single parameter info, expect JSON");
	        GetMethod get = new GetMethod(getTestHost()+ACTION_PARAMETER+"/1");
	        get.addRequestHeader("Accept" , "application/json");
	        HttpClient httpclient = new HttpClient();
	       
	        try {
	            int result = httpclient.executeMethod(get); 
	            System.out.println("Response status code: " + result);
	            System.out.println("Response body: ");
	            System.out.println(get.getResponseBodyAsString());
	        } finally {
	            get.releaseConnection();             
	        }
	    }
	    private static void testRequestJsonPutParameter()throws Exception{
	    	System.out.println("\n");
	        System.out.println("Sent HTTP POST request to save parameter info, use JSON, expect RESPONSE");
	        PostMethod post = new PostMethod(getTestHost()+ACTION_SAVEPARAMETER);
	        post.addRequestHeader("Accept" , "application/json");
	        HttpClient httpclient = new HttpClient();

	        try {
	        	
	        	
	        	post.setRequestEntity(new StringRequestEntity(" {\"Parameter\":[{\"id\":0,\"name\":\"DEMO1\",\"type\":1}]}"));

	            int result = httpclient.executeMethod(post);
	            
	            System.out.println("Response status code: " + result);
	            System.out.println("Response body: ");
	            System.out.println(post.getResponseBodyAsString());
	        } finally {
	            post.releaseConnection();            
	        }
	    }

	    private static void testRequestJsonRemoveParameter()throws Exception{
	    	System.out.println("\n");
	        System.out.println("Sent HTTP POST request to remove parameter, use JSON, expect RESPONSE");
	        PostMethod post = new PostMethod(getTestHost()+ACTION_REMOVEPARAMETER+"/1");
	        post.addRequestHeader("Accept" , "application/json");
	        HttpClient httpclient = new HttpClient();

	        try {
	        	
	            int result = httpclient.executeMethod(post);
	            
	            System.out.println("Response status code: " + result);
	            System.out.println("Response body: ");
	            System.out.println(post.getResponseBodyAsString());
	        } finally {
	            post.releaseConnection();            
	        }
	    }
}
